import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import store from '../store'

Vue.use(VueRouter);

const routes = [
  // 首页
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  // 直播
  {
    path: "/video",
    name: "video",
    component: () =>
      import("../views/VideoView.vue")
  },
  // 分类
  {
    path: "/type",
    name: "type",
    component: () =>
      import("../views/TypeView.vue"),
  },
  // 聊天
  {
    path: "/talk",
    name: "talk",
    component: () =>
      import("../views/TalkView.vue"),
    beforeEnter: (to, from, next) => {
      let token = store.state.token;
      if (token) {
        next()
      } else {
        next({ name: "login", params: { id: "talk" } })
      }
    }
  },
  // 我的
  {
    path: "/mine",
    name: "mine",
    component: () =>
      import("../views/MineView.vue"),
    beforeEnter: (to, from, next) => {
      let token = store.state.token;
      if (token) {
        next()
      } else {
        next({ name: "login", params: { id: "mine" } })
      }
    }
  },
  // 登录页
  {
    path: "/login",
    name: "login",
    component: () => import("../views/LoginView.vue")
  },
];

const router = new VueRouter({
  routes,
});

export default router;
